<ul class="submenu">
<?php if($this->Session->read('Auth.User.role') =='managers' || $this->Session->read('Auth.User.role') =='special' || $this->Session->read('Auth.User.role') =='admin'): ?>
    <li><?php echo $this->Html->link('All timesheets',array('controller' => 'timesheets', 'action' => 'indexall')); ?></li>  
<?php endif; ?>
	<li><?php echo $this->Html->link('Change password',array('controller' => 'users', 'action' => 'changepassword', $this->Session->read('Auth.User.id'))); ?></li>
</ul>

<?php
$launchdate = strtotime('2013-07-07');//has to be a sunday
$sevendays = 7 * 24 * 3600;
$starthdate = $launchdate + $sevendays;

$today = strtotime(date("Y-m-d"));

$weeks = array();
while($starthdate < $today){
	$starthdate = $starthdate + $sevendays;
	$weekending = date('Y-m-d',$starthdate);
	$weeks[$weekending] = $weekending;
}

krsort($weeks);//reverse the array order so that it picks dates from newest to oldest dates
$timesheetsrearranged = array();
foreach($timesheets as $timesheet):
	$timesheetsrearranged[$timesheet['Timesheet']['title']] = $timesheet;
endforeach; 

$count = 0;

/*echo '<pre>';
print_r($timesheetsrearranged);
print_r($weeks);
echo '</pre>';*/
?>


<table>
    <tr>
        <th>Week Ending</th>
        <th>Action</th>
        <th>Modified</th>
        <th>Created</th>
    </tr>
	<?php
	foreach($weeks as $starthdate => $weekending):
		if(array_key_exists($starthdate,$timesheetsrearranged)){ ?>
			<tr>
			<td><?php echo $this->Html->link(date('F j, Y',strtotime($timesheetsrearranged[$starthdate]['Timesheet']['title'])), array('controller' => 'timesheets', 'action' => 'edit', $timesheetsrearranged[$starthdate]['Timesheet']['id'])); ?></td>
            <td><?php echo $this->Html->link('edit', array('controller' => 'timesheets', 'action' => 'edit', $timesheetsrearranged[$starthdate]['Timesheet']['id'])); ?></td>
			<td><?php echo $timesheetsrearranged[$starthdate]['Timesheet']['modified']; ?></td>
            <td><?php echo $timesheetsrearranged[$starthdate]['Timesheet']['created']; ?></td>
            </tr>
        <?php
		}else{?>
			<tr>
			<td><?php echo $this->Html->link(date('F j, Y',strtotime($starthdate)), array('controller' => 'timesheets', 'action' => 'add', strtotime($starthdate))); ?></td>
            <?php
			if($count>0){ // this means that it is a past timesheet and has not been filled in ?>
				<td><?php echo $this->Html->link('add', array('controller' => 'timesheets', 'action' => 'add', strtotime($starthdate))); ?> - <?php echo $this->Html->image('warning.jpg', array('alt' => 'Warning', 'border' => '0', 'width' => '20', 'align' => 'absmiddle')) ?>: timesheet overdue</td>
			<?php
			}else{ //latest status ?>
				<td><?php echo $this->Html->link('add', array('controller' => 'timesheets', 'action' => 'add', strtotime($starthdate))); ?></a></td>
			
            <?php 
			}?>
            <td>0000-00-00</td>
            <td>0000-00-00</td>
			</tr> 
	<?php } 
		  $count++; ?>
	<?php endforeach; ?>
    <?php unset($weeks); ?>
  
</table>